package com.mall.coupon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.mall.coupon.entity.CouponEntity;
import com.mall.coupon.service.CouponService;
import com.mall.common.utils.Result;


@RefreshScope
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    @Value("${coupon.name}")
    String name;

    @GetMapping("/name")
    public Result name() {
        return Result.ok(name);
    }

    @GetMapping("/member/list")
    public Result membercoupons() {
        CouponEntity coupon = new CouponEntity();
        coupon.setCouponName("满100减10");
        return Result.ok().put("coupons", List.of(coupon));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list() {
        return Result.ok().put("page", couponService.list());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        CouponEntity coupon = couponService.getById(id);

        return Result.ok().put("coupon", coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody CouponEntity coupon) {
        couponService.save(coupon);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CouponEntity coupon) {
        couponService.updateById(coupon);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        couponService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }

}
